package com.ym521.emojipack;


import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.EditText;
import android.widget.FrameLayout;

import androidx.annotation.IntDef;
import androidx.fragment.app.FragmentActivity;

import com.ym521.emojipack.keyboard.fragment.EmojiBoardFragment;
import com.ym521.emojipack.util.Utils;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
 * @author Ym
 * E-mail: 2435970206@qq.com
 * createTime:2023/7/9
 * explain: emoji 自定义键盘
 */
public final class EmojiPackKeyBoard {

    public static final int THEME_SIMPLE = 1;
    public static final int THEME_DEFAULT = 0;

    @IntDef({THEME_DEFAULT, THEME_SIMPLE})
    @Retention(RetentionPolicy.SOURCE)
    public @interface BoardTheme { }

    public static Builder build(FragmentActivity activity) {
        return new Builder(activity,THEME_DEFAULT);
    }

    public static Builder build(FragmentActivity activity,@BoardTheme int theme) {
        return new Builder(activity,theme);
    }

    public static final class Builder {
        private EmojiBoardFragment fragment;
        private FragmentActivity activity;
        private View showView;

        public Builder(FragmentActivity activity,int theme) {
            this.activity = activity;
            this.fragment = EmojiBoardFragment.newInstance(theme);
        }

        /**
         * @param view 推荐控件容器 FrameLayout
         */
        public Builder bindView(FrameLayout view) {
            this.showView = view;
            showView.setVisibility(View.GONE);
            ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
            layoutParams.height = Utils.dip2px(view.getContext(), 250f);
            layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
            view.setLayoutParams(layoutParams);
            activity.getSupportFragmentManager()
                    .beginTransaction()
                    .replace(view.getId(), fragment)
                    .commit();
            return this;
        }

        /**
         * @param inputView 推荐控件容器 输入文件 EditText
         */
        public Builder setInputView(EditText inputView) {
            fragment.input(inputView);
            return this;
        }


        public Builder show() {
            Utils.hideInput(activity);
            showView.setVisibility(View.VISIBLE);
            showView.startAnimation(showAnimation());
            return this;
        }

        public Builder hide() {
            showView.startAnimation(hideShowAnimation());
            showView.setVisibility(View.GONE);
            return this;
        }

        private TranslateAnimation showAnimation() {
            //向上位移显示动画  从自身位置的最下端向上滑动了自身的高度
            TranslateAnimation translateAniShow = new TranslateAnimation(
                    Animation.RELATIVE_TO_SELF,//RELATIVE_TO_SELF表示操作自身
                    0,//fromXValue表示开始的X轴位置
                    Animation.RELATIVE_TO_SELF,
                    0,//fromXValue表示结束的X轴位置
                    Animation.RELATIVE_TO_SELF,
                    1,//fromXValue表示开始的Y轴位置
                    Animation.RELATIVE_TO_SELF,
                    0);//fromXValue表示结束的Y轴位置
            translateAniShow.setRepeatMode(Animation.REVERSE);
            translateAniShow.setDuration(200);
            translateAniShow.setInterpolator(new AccelerateDecelerateInterpolator());
            return translateAniShow;
        }

        private TranslateAnimation hideShowAnimation() {
            //向下位移隐藏动画  从自身位置的最上端向下滑动了自身的高度
            TranslateAnimation translateAniHide = new TranslateAnimation(
                    Animation.RELATIVE_TO_SELF,//RELATIVE_TO_SELF表示操作自身
                    0,//fromXValue表示开始的X轴位置
                    Animation.RELATIVE_TO_SELF,
                    0,//fromXValue表示结束的X轴位置
                    Animation.RELATIVE_TO_SELF,
                    0,//fromXValue表示开始的Y轴位置
                    Animation.RELATIVE_TO_SELF,
                    1);//fromXValue表示结束的Y轴位置
            translateAniHide.setRepeatMode(Animation.REVERSE);
            translateAniHide.setDuration(200);
            translateAniHide.setInterpolator(new AccelerateInterpolator());
            return translateAniHide;
        }
    }
}
